﻿using UnityEngine;


public class AssetTask : LoadTask
{
    /// <summary>
    /// 是否从AssetBundle中异步加载Asset
    /// </summary>
    public static bool async = true;

    // 加载bundle所占的进度比重
    const float BUNDLE_PERCENT = 0.8f;

    /// <summary>
    /// 指定加载bundle中的哪个asset
    /// </summary>
    public string name;
    /// <summary>
    /// 加载bundle任务
    /// </summary>
    public LoadTaskR bundleTask;
    /// <summary>
    /// 加载asset任务
    /// </summary>
    public AssetBundleRequest request;
    
    /// <summary>
    /// 上次使用时间
    /// </summary>
    public float lastUseTime;

    public string m_Error;
    /// <summary>
    /// 加载错误信息
    /// </summary>
    public override string error
    {
        get { return m_Error; }
    }

    public bool m_IsDone = false;
    /// <summary>
    /// 是否加载完成
    /// </summary>
    public override bool isDone
    {
        get { return m_IsDone; }
    }

    /// <summary>
    /// 加载进度
    /// </summary>
    public override float progress
    {
        get
        {
            if (isDone)
                return 1;

            if (request != null)
                return Mathf.Lerp(BUNDLE_PERCENT, 1, request.progress);

            if (bundleTask != null)
                return Mathf.Lerp(0, BUNDLE_PERCENT, bundleTask.progress);

            return 0;
        }
    }
    
    /// <summary>
     /// 更新入口
     /// </summary>
    public override void Update()
    {
        if (isDone)
            return;

        if (request != null)
        {
            if (request.isDone)
            {
                resource = request.asset;
                request = null;

                if (resource == null)
                    m_Error = string.Format("asset {0} not exist!", name);
                OnFinish();
            }
        }
        else if (bundleTask != null)
        {
            if (bundleTask.isDone)
            {
                if (!string.IsNullOrEmpty(bundleTask.error))
                {
                    m_Error = bundleTask.error;
                    OnFinish();
                }
                else
                {
                    var bundle = bundleTask.resource as AssetBundle;
                    if (bundle == null)
                    {
                        m_Error = "bundle not exist! " + bundleTask.taskID;
                        OnFinish();
                    }
                    else
                    {
                        var tag1 = name;
                        var names = bundle.GetAllAssetNames();
                        if (string.IsNullOrEmpty(tag1))
                        {
                            tag1 = names[0];
                        }

                        if (async)
                        {
                            request = bundle.LoadAssetAsync(tag1, resourceType);
                        }
                        else
                        {
                            resource = bundle.LoadAsset(tag1, resourceType);
                            if (resource == null)
                                m_Error = string.Format("asset {0} not exist!", name);
                            OnFinish();
                        }
                    }
                }
            }
        }
    }

    protected override void OnFinish()
    {
        m_IsDone = true;
        base.OnFinish();
    }

    /// <summary>
    /// 销毁对象，释放资源
    /// </summary>
    public override void Destroy()
    {
        if (bundleTask != null)
        {
            bundleTask.Unload();
            bundleTask = null;
        }
        base.Destroy();
    }
    
    /// <summary>
    /// 强制设定Asset加载完毕
    /// </summary>
    public void SetDone(Object asset)
    {
        this.resource = asset;
        OnFinish();
    }

    /// <summary>
    /// 判断路径是否相同
    /// </summary>
    public bool PathEquals(string path, string name, LoadSource source)
    {
        if (!PathEquals(path, source))
            return false;

        if (string.IsNullOrEmpty(this.name) || string.IsNullOrEmpty(name))
            return true;

        return string.Equals(this.name, name, System.StringComparison.OrdinalIgnoreCase);
    }
}
